<?php
class Czb {
    private $url = 'https://mcs.czb365.com/services/v3/';

    private $params = [
        'app_key' => 'juzhe1.0',
        'app_secret' => '46ed446dd17669fd812d8cc838790073',
    ];

    private $channelId = 92657226;

    public function __construct()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $this->params['timestamp'] = $msectime;
    }

    /**
     * 平台授权登录
     */
    public function login_simple($phone){


        $url = $this->url.'begin/platformLoginSimpleAppV4?';
        $this->params['platformType'] = $this->channelId;
        $this->params['platformCode'] = $phone;
        $sign = $this->sign($this->params);
        $this->params['sign'] = $sign;
        $url .= http_build_query($this->params);
        $res = file_get_contents($url);
        $res = json_decode($res,true);
        unset($this->params['platformCode']);
        return $res;
    }

    /**
     * 油站列表
     * @return false|mixed|string
     */
    public function oil_lists(){
        $url =  $this->url.'gas/queryGasInfoListOilNoNew?';
        $this->params['channelId'] = $this->channelId;
        $sign = $this->sign($this->params);
        $this->params['sign'] = $sign;
        $url .= http_build_query($this->params);
        $res = file_get_contents($url);
        $res = json_decode($res,true);
        return $res;
    }

    /**
     * 油站详情
     * @param $phone
     * @return false|mixed|string
     */
    public function oil_details($phone,$gasIds){
        $url =  $this->url.'gas/queryPriceByPhone?';
        $params['app_key'] = $this->params['app_key'];
        $params['app_secret'] = $this->params['app_secret'];
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $params['timestamp'] = $msectime;
        $params['gasIds'] = $gasIds;
        $params['platformType'] = $this->channelId;
        $params['phone'] = $phone;
        $sign = $this->sign($params);
        $params['sign'] = $sign;
        $url .= http_build_query($params);
        $res = file_get_contents($url);
        $res = json_decode($res,true);
        return $res;
    }

    /**
     * 签名
     * @param $params
     * @return string
     */
    private function sign($params){
        ksort($params);
        $str = '';
        foreach ($params as $key=>$val){
            $str .= $key.$val;
        }
        return md5($params['app_secret'].$str.$params['app_secret']);
    }

    /**
     * 订单列表
     */
    public function order_list($page,$size,$phone=''){
        $url =  $this->url.'orderws/platformOrderInfoV2?';
        $this->params['orderSource'] = $this->channelId;
        $this->params['phone'] = $phone;
        $this->params['pageIndex'] = $page;
        $this->params['pageSize'] = $size;
        $sign = $this->sign($this->params);
        $this->params['sign'] = $sign;
        $url .= http_build_query($this->params);
        $res = file_get_contents($url);
        $res = json_decode($res,true);
        return $res;
    }

}
